# -*- coding: utf-8 -*-
from typing import Annotated

from fastapi_limiter.depends import RateLimiter
from fastapi import APIRouter, Path, Query

from App.common.pagination import DependsPagination, paging_data
from App.common.respones.response_schema import ResponseModel, response_base
from App.common.security.jwt import DependsJwtAuth
from App.database.mysql import CurrentSession

# 动态导入模型和Schema
from {{ service_import_path }} import {{ table_name_class }}Service
from {{ schema_import_path }} import (
    Create{{ schema_name }}Param,
    Update{{ schema_name }}Param,
    Get{{ table_name_class  }}ListDetails
)


router = APIRouter()


@router.get('/{pk}',
    summary='获取{{ table_name_class }}详情',
    description='获取{{ table_name_class }}详情',
    dependencies=[Depends(RateLimiter(times=5, minutes=1)), DependsJwtAuth]
)
async def get_{{ table_name_en }}(pk: Annotated[int, Path(...)]) -> ResponseModel:
    {{ table_name_en }} = await {{ table_name_class }}Service.get(pk=pk)
    return response_base.success(data={{ table_name_en }})


@router.get(
    '',
    summary='（模糊条件）分页获取所有{{ table_name_class }}',
    description='（模糊条件）分页获取所有{{ table_name_class }}',
    dependencies=[
        Depends(RateLimiter(times=5, minutes=1)),
        DependsJwtAuth,
        DependsPagination
    ]
)
async def get_pagination_{{ table_name_en }}(db: CurrentSession) -> ResponseModel:
    {{ table_name_en }}_select = await {{ table_name_class }}Service.get_select()
    page_data = await paging_data(db, {{ table_name_en }}_select, Get{{ schema_name }}ListDetails)
    return response_base.success(data=page_data)


@router.post(
    '',
    summary='创建{{ table_name_class }}',
    description='创建{{ table_name_class }}',
    dependencies=[Depends(RateLimiter(times=5, minutes=1)), DependsJwtAuth]
)
async def create_{{ table_name_en }}(obj_in: Create{{ schema_name }}Param) -> ResponseModel:
    data = await {{ table_name_class }}Service.create(obj_in=obj_in)
    return response_base.success(data=data)


@router.put(
    '',
    summary='更新{{ table_name_class }}',
    description='更新{{ table_name_class }}',
    dependencies=[Depends(RateLimiter(times=5, minutes=1)), DependsJwtAuth]
)
async def update_{{ table_name_en }}(obj_in: Update{{ schema_name }}Param) -> ResponseModel:
    count = await {{ table_name_class }}Service.update(obj_in=obj_in)
    if count > 0:
        return response_base.success()
    return response_base.fail()


@router.delete(
    '/{pk}',
    summary='删除{{ table_name_class }}',
    description='删除{{ table_name_class }}',
    dependencies=[Depends(RateLimiter(times=5, minutes=1)), DependsJwtAuth]
)
async def delete_{{ table_name_en }}(pk: [int] = Path(...)) -> ResponseModel:
    count = await {{ table_name_class }}Service.delete(pk=pk)
    if count > 0:
        return response_base.success()
    return response_base.fail()